package com.zhuang.singleton.type1;

/**
 * @Classname SingletonTest01
 * @Description  饿汉式(静态变量)
 * @Date 2021/3/17 9:26
 * @Created by dell
 */

public class SingletonTest01 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        //判断是否为单例
        System.out.println(instance == instance2);
        System.out.println("intstance的哈希值" + instance.hashCode());
        System.out.println("intstance2的哈希值" + instance2.hashCode());
    }
}

class Singleton {

    //1.构造器私有化，外部能new、
    private Singleton() {

    }

    //本类内部创建对象实例
    private final static Singleton instance = new Singleton();

    //对外部提供一个公有的静态方法
    public static Singleton getInstance() {
        return instance;
    }
}